package com.billdowney.qq.robot.event;

import com.billdowney.qq.robot.bean.MessageEventBean;
import com.billdowney.qq.robot.bean.QQjiqiren.jiankongqun;
import com.billdowney.qq.robot.bean.QQjiqiren.tuisongxiaoxi;
import com.billdowney.qq.robot.controller.qqjiqirenController;
import com.billdowney.qq.robot.enums.RobotEventEnum;
import com.billdowney.qq.robot.mapper.QQjiqiren.JinqunMapper;
import com.billdowney.qq.robot.util.ShiJianUtil;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Member;
import net.mamoe.mirai.event.Event;
import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.ListeningStatus;
import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.event.events.FriendMessageEvent;
import net.mamoe.mirai.event.events.GroupMessageEvent;
import net.mamoe.mirai.event.events.MemberJoinEvent;
import net.mamoe.mirai.event.events.MemberLeaveEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author BillDowney
 * @date 2021/3/30 21:08
 */
@Slf4j
@Component
public class EventListeningHandle extends SimpleListenerHost {

    @Autowired
    private ApplicationEventPublisher publisher;

    @Autowired
    SendMessageEventHandle sendMessageEventHandle;
    @Autowired
    qqjiqirenController qqjiqirenController;

    @Autowired
     JinqunMapper jinqunMapper;

    /**
     * 监听好友消息
     *
     * @param event 好友消息
     * @return {@link ListeningStatus}
     */
    @EventHandler
    public ListeningStatus onFriendMessageEvent(FriendMessageEvent event) {
        this.publishMessage(RobotEventEnum.FRIEND_MSG, event);
        // 保持监听
        return ListeningStatus.LISTENING;
    }

    /**
     * 监听群消息
     *
     * @param event 群消息
     * @return {@link ListeningStatus}
     */
//    @EventHandler
//    public ListeningStatus onGroupMessageEvent(GroupMessageEvent event) {
//        this.publishMessage(RobotEventEnum.GROUP_MSG, event);
//        // 保持监听
////        MessageEventBean xiaoxinti  =new MessageEventBean();
////        sendMessageEventHandle.message(xiaoxinti);
//        return ListeningStatus.LISTENING;
//    }

    @EventHandler
    public ListeningStatus onGroupMessageEvent(GroupMessageEvent event) {
//        this.publishMessage(RobotEventEnum.GROUP_MSG, event);
        // 保持监听
        try {
            qqjiqirenController.s1s(event);
        } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


//        MessageEventBean xiaoxinti  =new MessageEventBean();
//        sendMessageEventHandle.message(xiaoxinti);
        return ListeningStatus.LISTENING;
    }





    /**
     * 监听入群消息
     *
     * @param event 群消息
     * @return {@link ListeningStatus}
     */
    @EventHandler
    public ListeningStatus onMemberJoinEvent(MemberJoinEvent event) {
        this.publishMessage(RobotEventEnum.MEMBER_JOIN, event);
        Member member = event.getMember();
        Bot bot = event.getBot();
        long groupId = event.getGroupId();
        Member user = event.getUser();
        jiankongqun jinqunxiaoxi   =new jiankongqun();
        jinqunxiaoxi.setCanzuozhe(String.valueOf(user.getId()));
        jinqunxiaoxi.setQqhao(String.valueOf(member.getId()));
        jinqunxiaoxi.setQqqunhao(String.valueOf(groupId));
        jinqunxiaoxi.setShijian(ShiJianUtil.shijianshifenmiao(new Date()));
        jinqunxiaoxi.setShijian1("加入群");
        jinqunxiaoxi.setJiankongqq(String.valueOf(bot.getId()));
        jinqunxiaoxi.setMingcheng(member.getNameCard());
        jinqunMapper.Sel(jinqunxiaoxi);

        tuisongxiaoxi tu =new tuisongxiaoxi();
        tu.setMQ_fromID(String.valueOf(groupId));
        tu.setMQ_robot(String.valueOf(bot.getId()));
        tu.setMQ_msgID(String.valueOf(groupId));
        qqjiqirenController.进群提示语(tu);

        // 保持监听
        return ListeningStatus.LISTENING;
    }

    /**
     * 监听退群消息
     *
     * @param event 群消息
     * @return {@link ListeningStatus}
     */
    @EventHandler
    public ListeningStatus onMemberLeaveEvent(MemberLeaveEvent event) {
        this.publishMessage(RobotEventEnum.MEMBER_LEAVE, event);
        Member member = event.getMember();
        Bot bot = event.getBot();
        long groupId = event.getGroupId();
        Member user = event.getUser();
        jiankongqun jinqunxiaoxi   =new jiankongqun();
        jinqunxiaoxi.setCanzuozhe(String.valueOf(user.getId()));
        jinqunxiaoxi.setQqhao(String.valueOf(member.getId()));
        jinqunxiaoxi.setQqqunhao(String.valueOf(groupId));
        jinqunxiaoxi.setShijian(ShiJianUtil.shijianshifenmiao(new Date()));
        jinqunxiaoxi.setShijian1("退出群");
        jinqunxiaoxi.setJiankongqq(String.valueOf(bot.getId()));
        jinqunxiaoxi.setMingcheng(member.getNameCard());
        jinqunMapper.Sel(jinqunxiaoxi);

        // 保持监听
        return ListeningStatus.LISTENING;
    }

    private void publishMessage(RobotEventEnum eventType, Event event) {
        MessageEventBean bean = new MessageEventBean();
        bean.setRobotEventEnum(eventType);
        bean.setEvent(event);
        publisher.publishEvent(bean);
    }
}
